package com.shopping.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.shopping.product.entity.AttrEntity;
import com.shopping.product.service.AttrAttrgroupRelationService;
import com.shopping.product.service.AttrGroupService;
import com.shopping.product.service.AttrService;
import com.shopping.product.service.CategoryService;
import com.shopping.product.vo.AttrGroupRelationVo;
import com.shopping.product.vo.AttrGroupWhitAttrsVo;
import com.shopping.product.vo.AttrVo;
import org.springframework.web.bind.annotation.*;

import com.shopping.product.entity.AttrGroupEntity;
import com.shopping.common.utils.PageUtils;
import com.shopping.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author Europa
 * @email qw1124sdo8@live.com
 * @date 2020-04-11 22:49:19
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    AttrService attrService;

    @Resource
    AttrAttrgroupRelationService relationService;

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWhitAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }

    /**
     * 添加属性与分组关联关系
     *
     * @param vos 含AttrGroupRelationVo的list
     * @return 状态码 + 操作信息
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 获取属性分组的关联的所有属性
     *
     * @param attrgroupId 属性分组id
     * @return 状态码 + 操作信息 + 含AttrEntity的list
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entityList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entityList);
    }

    /**
     * 获取属性分组没有关联的其他属性
     *
     * @param attrgroupId 分组id
     * @param params
     * @return 状态码 + 操作信息 + 含AttrEntity的list的分页
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        System.out.println("params = " + params);
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 批量删除属性与分组的关联关系
     *
     * @param vos AttrGroupRelationVo[]
     * @return 状态码 与 操作信息
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 获取分类属性分组
     *
     * @param params
     * @param catelogId catelog_id
     * @return 状态码 与 操作信息
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 获取属性分组详情
     *
     * @param attrGroupId 属性分组id
     * @return 状态码 + 操作信息 + AttrGroupEntity实体
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrEntity) {
        attrGroupService.save(attrEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:updateSort")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
